package laba1;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

/**
 * Created by Михаил on 19.03.14.
 */
public class Main {
    public static void main(String[] args) throws IOException{
        //TODO:
        //Tests
        Scanner in = null;
        FileWriter out = null;
        try {
            String inFileName = args[0], outFileName = args[1];
            in = new Scanner(new BufferedReader(new FileReader(inFileName)));
            out = new FileWriter(outFileName);

            //initialization
            String command;
            Integer el;
            MyLinkedDeque mld = new MyLinkedDeque();

            while (in.hasNext()) {
                command = in.next();
                if (command.equals("addFirst")) {
                    el = in.nextInt();
                    mld.addFirst(el);
                    out.write(mld.toString() + "\n");
                } else if (command.equals("addLast")) {
                    el = in.nextInt();
                    mld.addLast(el);
                    out.write(mld.toString() + "\n");
                } else if (command.equals("getFirst")) {
                    out.write(mld.getFirst() + "\n" + mld.toString() + "\n");
                } else if (command.equals("getLast")) {
                    out.write(mld.getLast() + "\n" + mld.toString() + "\n");
                } else if (command.equals("pollFirst")) {
                    out.write(mld.pollFirst() + "\n" + mld.toString() + "\n");
                } else if (command.equals("pollLast")) {
                    out.write(mld.pollLast() + "\n" + mld.toString() + "\n");
                } else if (command.equals("peekFirst")) {
                    out.write(mld.peekFirst() + "\n" + mld.toString() + "\n");
                } else if (command.equals("peekLast")) {
                    out.write(mld.peekLast() + "\n" + mld.toString() + "\n");
                } else if (command.equals("size")) {
                    out.write(mld.size() + "\n");
                } else if (command.equals("isEmpty")) {
                    if (mld.isEmpty()) out.write("Deque is empty.\n");
                    else out.write("Deque is not empty.\n");
                } else if (command.equals("clear")) {
                    mld.clear();
                    out.write(mld.toString() + "\n");
                } else if (command.equals("toString")) {
                    out.write(mld.toString() + "\n");
                } else out.write("Unknown command: " + command + "\n");
            }

        } catch (FileNotFoundException  e) {
            System.out.print("Ошибка открытия файла:\n" + e.getMessage());
        } catch (IOException e) {
            System.out.print("Ошибка записи в файл:\n" + e.getMessage());
        } finally {
            if (in != null) in.close();
            if (out != null) out.close();
        }
    }
}
